/**
 * Created by MDURAND on 16/10/2014.
 */

helloseriesApp.controller("LoginCtrl", ['$scope', '$log', 'AuthService' ,'PopupService', 'CredentialModel', 'UserModel',
    function ( $scope, $log, AuthService, PopupService, CredentialModel, UserModel) {
        $scope.credentials = new CredentialModel();

        $scope.login = function (credentials) {

            AuthService.login(credentials).then(function ( retour) {
                // SUCCESS
                $scope.formClass = "has-success";

                if (retour.data.code == 0)
                {
                    AuthService.setCurrentUser (new UserModel(retour.data.user ) );
                    $scope.user =  AuthService.getCurrentUser();
                }
                else
                {
                    $scope.formClass = "has-error";
                }

            }, function () {
                AuthService.setCurrentUser( {} );
                PopupService.showPopupError();
            });
        };

    }
] );


